﻿using System;
using ExtraClub.Domain;
using ExtraClub.Domain.Entities;
using SubSonic;
using System.Collections.Generic;
using ExtraClub.Domain.Framework;
using System.Reflection;

namespace ExtraClub.Web.Controls.client_grid {
    public partial class module : BaseModule {
        protected List<VwClientProfile> clients = null;
        protected Club SelectedClub{ get; set; }

        public List<string> ColumnCollection { get; set; }
        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            Query q = VwClientProfile.Query();

            if (Request.QueryString.Count > 0 && Request.QueryString["clubid"] != null) {
                SelectedClub = Club.FetchByID(int.Parse(Request.QueryString["clubid"]));
                q.AddWhere(VwClientProfile.Columns.ParentId, SelectedClub.Id);
            } else {
                q.AddWhere(VwClientProfile.Columns.ParentId, SiteContext.Current.Club.Id);
            }

            string key = "client_grid_filter";
            Dictionary<string, object> data = Items.ContainsKey("data") ? (Dictionary<string, object>)Items["data"] : (Dictionary<string, object>)Session[key];
            if (data != null) {
                SiteContext.Current.SetLanguage(data["lang"].ToString());
                q.AddWhere(VwClientProfile.Columns.FirstName, Comparison.Like, "%" + data[VwClientProfile.Columns.FirstName.ToLower()].ToString().Trim().ToLower() + "%");

                Session[key] = data;
            }

            VwClientProfileCollection list = new VwClientProfileCollection();
            list.LoadAndCloseReader(VwClientProfile.FetchByQuery(q));
            clients = list.GetList();

            AddMailForm.LoadForm(RequestedForm);
        }
    }
}